package com.haoshuang.zhouzhoubang.ui.page.mine.wallet

import android.content.Context
import android.os.Bundle
import com.haoshuang.zhouzhoubang.databinding.ActivityMyWalletBinding
import com.haoshuang.zhouzhoubang.ui.activity.BalanceDetailsActivity
import com.haoshuang.zhouzhoubang.ui.activity.BalanceWithdrawalActivity
import com.haoshuang.zhouzhoubang.ui.activity.BalanceWithdrawalDetailActivity
import com.haoshuang.zhouzhoubang.ui.activity.TeamBzActivity
import com.haoshuang.zhouzhoubang.ui.activity.WalletDetailsActivity
import com.haoshuang.zhouzhoubang.ui.dialog.ConfirmDialog
import com.haoshuang.zhouzhoubang.ui.page.mine.wallet.frozen.FrozenAmountActivity
import com.lxj.xpopup.XPopup
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger

/**
 *  我的钱包
 */
class MyWalletActivity : BaseVMActivity<MyWalletViewModel, ActivityMyWalletBinding>() {

    companion object {
        fun launch(context: Context){
            context.startPage<MyWalletActivity>()
        }
    }
    var deposit_money = ""
    var price = ""
    override fun initView(savedInstanceState: Bundle?) {
        binding.titleBar.title = "我的钱包"
        binding.titleBar.leftImageView().click { finish() }

        binding.apply {
            llJd.clickWithTrigger {
                XPopup.Builder(mActivity)
                    .asCustom(ConfirmDialog(
                        context = mActivity,
                        title = "总金额说明",
                        content = "账户总金额=可用余额+保证金余额+冻结金额",
                        cancelStr = "",
                        confirmStr = "我知道了",
                        isHideCancel = true,
                        onCancel = {},
                        onConfirm = {}))
                    .show()
            }

            // 冻结金额
            llJd2.clickWithTrigger {
                startPage<FrozenAmountActivity>()
            }

            // 提现记录
            cevBalanceWithdrawal.setOnClickListener {
                startPage<BalanceWithdrawalDetailActivity>()
            }

            // 提现
            cevBalanceWithdrawal2.setOnClickListener {
                startPage<BalanceWithdrawalActivity>("price" to price)
            }

            // 保证金
            llBzj.setOnClickListener {
                startPage<TeamBzActivity>()
            }

            cevBalanceDetails.clickWithTrigger {
                // 余额明细
                startPage<BalanceDetailsActivity>()
            }

            // 保证金明细
            cevMarginDetails.clickWithTrigger {
                startPage<WalletDetailsActivity>()
            }

        }
    }

    override fun initData() {

    }

    override fun onResume() {
        super.onResume()
        vm.getWalletDetail()
    }

    override fun startObserve() {
        vm.getWalletDetailData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {res ->
                val it = res?.data
                dismissLoading()
                binding.nowMoney.text = it?.frozen_money
                price = it?.use_money.toString()
                binding.frozenMoney.text = it?.use_money
                binding.nowMoney.text = it?.now_money
                binding.tvDj.text = it?.frozen_money
                deposit_money = it?.deposit_money.toString()
                binding.depositMoney.text = it?.deposit_money
            }
        }
    }

}